<template>
  <div>
    <el-form ref="cooperationForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="合同编号" prop="contractNo">
        <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
      </el-form-item>

      <el-form-item label="合作类型" prop="cooperationType">
        <el-select v-model="form.cooperationType" placeholder="请选择合作类型">
          <el-option label="长期合作" value="长期" />
          <el-option label="短期合作" value="短期" />
          <el-option label="项目合作" value="项目" />
        </el-select>
      </el-form-item>

      <el-form-item label="合作日期" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="handleDateChange"
        />
      </el-form-item>

      <el-form-item label="合作状态" prop="cooperationStatus">
        <el-select v-model="form.cooperationStatus" placeholder="请选择合作状态">
          <el-option label="正常" :value="1" />
          <el-option label="暂停" :value="2" />
          <el-option label="终止" :value="0" />
        </el-select>
      </el-form-item>

      <el-form-item label="优先级" prop="priorityLevel">
        <el-select v-model="form.priorityLevel" placeholder="请选择优先级">
          <el-option label="高" value="高" />
          <el-option label="中" value="中" />
          <el-option label="低" value="低" />
        </el-select>
      </el-form-item>

      <el-form-item label="付款条件" prop="paymentTerms">
        <el-input v-model="form.paymentTerms" placeholder="请输入付款条件，如：月结30天" />
      </el-form-item>

      <el-form-item label="信用额度" prop="creditLimit">
        <el-input-number v-model="form.creditLimit" :precision="2" :min="0" :step="1000" />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CooperationForm",
  props: {
    supplierId: {
      type: Number,
      required: true
    },
    cooperation: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dateRange: [],
      form: {
        supplierId: this.supplierId,
        contractNo: '',
        cooperationType: '',
        startDate: '',
        endDate: '',
        cooperationStatus: 1,
        priorityLevel: '中',
        paymentTerms: '',
        creditLimit: 0,
        remark: ''
      },
      rules: {
        contractNo: [
          { required: true, message: '请输入合同编号', trigger: 'blur' }
        ],
        cooperationType: [
          { required: true, message: '请选择合作类型', trigger: 'change' }
        ],
        dateRange: [
          { required: true, message: '请选择合作日期范围', trigger: 'change' }
        ],
        cooperationStatus: [
          { required: true, message: '请选择合作状态', trigger: 'change' }
        ],
        priorityLevel: [
          { required: true, message: '请选择优先级', trigger: 'change' }
        ]
      }
    };
  },
  watch: {
    cooperation: {
      handler(val) {
        if (val && Object.keys(val).length > 0) {
          this.form = { ...val };
          if (val.startDate && val.endDate) {
            this.dateRange = [val.startDate, val.endDate];
          }
        }
      },
      immediate: true,
      deep: true
    },
    supplierId: {
      handler(val) {
        this.form.supplierId = val;
      },
      immediate: true
    }
  },
  methods: {
    handleDateChange(val) {
      if (val) {
        this.form.startDate = val[0];
        this.form.endDate = val[1];
      } else {
        this.form.startDate = '';
        this.form.endDate = '';
      }
    },
    // 重置表单
    resetForm() {
      if (this.$refs.cooperationForm) {
        this.$refs.cooperationForm.resetFields();
      }
      this.dateRange = [];
      this.form = {
        supplierId: this.supplierId,
        contractNo: '',
        cooperationType: '',
        startDate: '',
        endDate: '',
        cooperationStatus: 1,
        priorityLevel: '中',
        paymentTerms: '',
        creditLimit: 0,
        remark: ''
      };
    },
    // 获取表单数据
    getFormData() {
      return this.form;
    },
    // 验证表单
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.cooperationForm.validate(valid => {
          if (valid) {
            resolve(this.form);
          } else {
            reject('表单验证失败');
          }
        });
      });
    }
  }
}
</script>
